package com.example.yls.wuziqi;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yls on 2018/6/13.
 */

public class WuziPanel extends View {
    private int panelWidth;
    private float itemSize;
    private int MAX_ITEMS = 10;
    private Paint paint;
    private Bitmap mWhitePiece;
    private Bitmap mBlackPiece;
    private List<Point> mWhiteArray = new ArrayList();
    private List<Point> mBlackArray = new ArrayList();
    private float radioOfPiect = 3.0f / 4 ;
    private boolean isWhite = true;


    public WuziPanel(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
        initBitMap();
        setBackgroundColor(0x44FF0000);
    }

    private void initBitMap() {
        mWhitePiece = BitmapFactory.decodeResource(getResources(), R.drawable.stone_w2);
        mBlackPiece = BitmapFactory.decodeResource(getResources(), R.drawable.stone_b1);
    }


    private void initPaint() {
        paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setDither(true);
        paint.setAntiAlias(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int width = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);

        int height = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        Log.e("WuziPanel", "onMeasure w =" + width + " h = " + height);

        int result = Math.min(width, height);
        setMeasuredDimension(result, result);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        Log.e("WuziPanel", "onSizeChanged  w = " + w + "  h  = " + h);
        panelWidth = w;
        itemSize = panelWidth * 1.0f / MAX_ITEMS;

        int pieceSize = (int) (itemSize * radioOfPiect);
        mWhitePiece = Bitmap.createScaledBitmap(mWhitePiece,pieceSize,pieceSize,false);
        mBlackPiece = Bitmap.createScaledBitmap(mBlackPiece,pieceSize,pieceSize,false);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_UP){
            int x = (int) event.getX();
            int y = (int) event.getY();
            Log.e("onTouchEvent", "x = " + x + "  y = " + y);

            Point p = getValuedPoint(x, y);
            Log.e("onTouchEvent", "p.x = " + p.x + "  p.y = " + p.y);
            if(mWhiteArray.contains(p) || mBlackArray.contains(p)){
                return  false;
            }

            if(isWhite){
                mWhiteArray.add(p);
            }else{
                mBlackArray.add(p);
            }
            postInvalidate();
            isWhite = !isWhite;


        }
        return true;
    }

    private Point getValuedPoint(int x, int y) {
        return new Point((int)(x/itemSize), (int)( y/itemSize));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawBoard(canvas);
        drawPieces(canvas);
    }

    private void drawPieces(Canvas canvas) {
        Log.e("drawPieces", "mWhiteArray.size() = " + mWhiteArray.size() + "  mBlackArray.size() " + mBlackArray.size());
        for(int i=0; i<mWhiteArray.size(); i++){
            Point p = mWhiteArray.get(i);
            int x = (int) ((p.x + 0.125f) * itemSize);
            int y = (int) ((p.y + 0.125f) * itemSize);
            canvas.drawBitmap(mWhitePiece, x, y, null);
        }

        for(int i=0; i<mBlackArray.size(); i++){
            Point p = mBlackArray.get(i);
            int x = (int) ((p.x + 0.125f) * itemSize);
            int y = (int) ((p.y + 0.125f) * itemSize);
            canvas.drawBitmap(mBlackPiece, x, y, null);
        }
    }

    private void drawBoard(Canvas canvas) {
        for(int i=0; i<MAX_ITEMS; i++){
            int startX = (int) (itemSize / 2);
            int endX = (int) (panelWidth - (itemSize / 2));
            int y = (int) ((0.5f + i) * itemSize);

            // 画十条横线
            canvas.drawLine(startX, y, endX, y, paint);
            // 画十条竖线
            canvas.drawLine(y,startX, y, endX, paint);
        }
    }
}
